//
// Ishtar C++ Utility Library
//
// Task Facility - Work Poller Header
//

#ifndef __ISHTAR_TASK_WORK_POLLER_H
#define __ISHTAR_TASK_WORK_POLLER_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <Ishtar/Task/TaskFwd.h>
#include <Ishtar/Task/WorkExecutor.h>


namespace Ishtar
{

namespace Task
{

///////////////////////////////////////////////////////////////////////////////
//
// Work Poller
//

class WorkPollerImpl;

class WorkPoller : public WorkExecutor
                 , public boost::noncopyable
{
public:

    WorkPoller();
    ~WorkPoller();


    //
    // Shutdown
    // - clear all waiting works.
    //   after this call, Submit() would throw and Process() do nothing.
    //

    void Shutdown();


    //
    // Submitting Works
    //
    
    void Submit( const Work& w );

    using WorkExecutor::Submit;


    /// properties about works ///

    Uint GetNumWorks()      const;
    Uint GetNumReadyWorks() const;


    //
    // Process
    // - Call to execute the works in this poller.
    //
    //   it executes works as many as possilbe, until the slice time (milliseconds) elapsed.
    //   if slice time is 0, it executes until no work left.
    //   returns how many works are executed.
    // 

    Uint Process( Uint sliceTime = 0 );

private:

    boost::scoped_ptr< WorkPollerImpl > m_impl;
};


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_WORK_POLLER_H
